/**
 * 安卓白金英雄坛制作组
 * <br>
 * 文件名：ControlScreen.java
 * <br>
 * 创建时间：2013-8-5 下午12:26:38
 * <br>
 * 所属项目：GmudEX
 * <br>
 *
 * @author 12548
 *         <br>
 */
package lostland.gmud.exv2.battle;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.orhanobut.logger.Logger;

import lostland.general.network.GmudHttp;
import lostland.general.network.GmudRequestListener;
import lostland.general.network.GmudRestUrl;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.netbattle.ClientViewScreen;
import lostland.gmud.exv2.battle.netbattle.NetworkMode;
import lostland.gmud.exv2.battle.proc.BattleStart;
import lostland.gmud.exv2.battle.proc.DummyStatus;
import lostland.gmud.exv2.battle.proc.FreeStatus;
import lostland.gmud.exv2.battle.proc.PreAttackStatus;
import lostland.gmud.exv2.battle.proc.StuntScreen;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.ui.InventoryScreen;
import lostland.gmud.exv2.ui.NotificationScreen;
import lostland.gmud.exv2.ui.core.MenuScreen;
import lostland.gmud.exv2.ui.core.NewButton;

/**
 * 类名：ControlScreen <br>
 * 说明：玩家的战斗控制屏（可选择攻击、吸气、逃跑等）
 *
 * @author 12548
 */
public class ControlScreen extends MenuScreen {

	public ControlScreen() {
		super(new BattleButton[]{
				new BattleButton(0),
				new BattleButton(1),
				new BattleButton(2),
				new BattleButton(3),
				new BattleButton(4),
				new BattleButton(5)
		});
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.MenuScreen#onClick(int)
	 */
	@Override
	protected void onClick(int index) {

		if (BattleScreen.getInstance().networkMode == NetworkMode.CLIENT) {

			RequestParams rParams = GmudHttp.initBattleParams();


			switch (index) {
				case 0:
					if (BattleScreen.getInstance().p1().getGauge() < GameConstants.ATTACK_GAUGE_COST) {
						new PureViewScreen("你的怒气值不足！").pushToGame();
						return;
					} else {
						rParams.addBodyParameter("action", 0 + "");
					}
					break;
				case 1:
					if (BattleScreen.getInstance().p1().getGauge() < GameConstants.STUNT_GAUGE_COST) {
						new PureViewScreen("你的怒气值不足！").pushToGame();
						return;
					} else {
						boolean flag = false;

						for (StuntData sd : StuntData.getInstance()) {
							boolean flag1 = true;

							if (sd.weaponType > -1 && sd.weaponType != BattleScreen.getInstance().p1().getAttackItem().getSubkind()) {
								flag1 = false;
								continue;
							}

							if (sd.talentNeeded > -1 && Gmud.mc.talent != sd.talentNeeded) {
								flag1 = false;
								continue;
							}

							for (int i = 0; i < 5; i++) {
								if (sd.equipSkillNeeded[i] > -1
										&& Gmud.mc.skillsckd[i] != sd.equipSkillNeeded[i]) {
									flag1 = false;
									break;
								}
							}

							if (flag1) {
								flag = true;
								break;
							}
						}

						if (flag) {
							new StuntScreen().replaceCurrent();
						}

					}
					return;
				case 2:
					// 吸气

					if (BattleScreen.getInstance().p1().getGauge() < GameConstants.XIQI_GAUGE_COST) {
						new PureViewScreen("你的怒气值不足！").pushToGame();
						return;
					} else {
						new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
						new DummyStatus().pushToBattle();
						if (BattleScreen.getInstance().p1().skillsckd[3] < 10) {
							new PureViewScreen("你必须选择要用的内功心法").replaceCurrent();
						} else if(BattleScreen.getInstance().p1().getSp() >= BattleScreen.getInstance().p1().getHp()) {
							new PureViewScreen("你现在体力充沛。").replaceCurrent();
						} else if (BattleScreen.getInstance().p1().getFp() == 0) {
							new PureViewScreen("你的内力不够。").replaceCurrent();
						} else {
							rParams.addBodyParameter("action", 2 + "");
						}
					}
					break;
				case 3:
					new PureViewScreen("网络对战目前不能使用物品！").pushToGame();
					return;
				case 4:
					rParams.addBodyParameter("action", 4 + "");
					break;
				case 5:
					new GmudHttp().postBackground(GmudRestUrl.ClientDrop, rParams, null);
					getGame().popToRootScreen();
					return;
				default:
					break;
			}


			new GmudHttp("正在发送数据......").postLocked(GmudRestUrl.ClientAction, rParams, new GmudRequestListener() {

				@Override
				public void onSucc(ResponseInfo<String> info) {
					ControlScreen.this.replaceBy(new ClientViewScreen());
					return;
				}

				@Override
				public void onFail(HttpException error, String msg) {
					getGame().popToRootScreen();
					new NotificationScreen("网络错误！！").pushToGame();
				}
			});

		} else {
			switch (index) {
				case 0:
					// 攻击
					if (BattleScreen.getInstance().p1().getDz() > 0) {
						BattleScreen.getInstance().p1().setDz(BattleScreen.getInstance().p1().getDz() - 1);

						new ViewScreen(BattleScreen.bsp(BattleScreen.getInstance().p1(),
								BattleScreen.getInstance().p2(), "$N现在呆若木鸡！(剩余回合"
										+ BattleScreen.getInstance().p1().getDz() + ")")).replaceCurrent();
					} else {
						if (BattleScreen.getInstance().p1().getGauge() < GameConstants.ATTACK_GAUGE_COST) {
							new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
							new DummyStatus().pushToBattle();
							new PureViewScreen("你的怒气值不足！").replaceCurrent();
						} else {
							if (BattleScreen.getInstance().p1().getGauge() < GameConstants.GAUGE_MAX) {
								BattleScreen.getInstance().p1().addGauge(-GameConstants.ATTACK_GAUGE_COST);
								BattleScreen.getInstance().atkprocess(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2(), null, null);
							} else {
								BattleScreen.getInstance().p1().addGauge(-GameConstants.GAUGE_MAX);
								PreAttackStatus pas = new PreAttackStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2(), null,
										"【全力一击】");
								pas.dmgBouns = 0.1f;
								pas.hitBouns = 0.1f;
								pas.pushToBattle();
							}
// game.pushScreen(Gmud.bs);
							getGame().popScreen();
						}
					}
					break;
				case 1:
					// 绝招
					if (BattleScreen.getInstance().p1().getDz() > 0) {
						BattleScreen.getInstance().p1().setDz(BattleScreen.getInstance().p1().getDz() - 1);

						new ViewScreen(BattleScreen.bsp(BattleScreen.getInstance().p1(),
								BattleScreen.getInstance().p2(), "$N现在呆若木鸡！(剩余回合"
										+ BattleScreen.getInstance().p1().getDz() + ")")).replaceCurrent();
					} else {
						if (BattleScreen.getInstance().p1().getGauge() < GameConstants.STUNT_GAUGE_COST) {
							new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
							new DummyStatus().pushToBattle();
							new PureViewScreen("你的怒气值不足！").replaceCurrent();
						} else {
							boolean flag = false;

							for (StuntData sd : StuntData.getInstance()) {
								boolean flag1 = true;

								if (sd.weaponType > -1 && sd.weaponType != BattleScreen.getInstance().p1().getAttackItem().getSubkind()) {
									flag1 = false;
									continue;
								}

								if (sd.talentNeeded > -1 && Gmud.mc.talent != sd.talentNeeded) {
									flag1 = false;
									continue;
								}


								for (int i = 0; i < 5; i++) {
									if (sd.equipSkillNeeded[i] > -1
											&& Gmud.mc.skillsckd[i] != sd.equipSkillNeeded[i]) {
										flag1 = false;
										break;
									}
								}


								if (flag1) {
									flag = true;
									break;
								}
							}


							if (flag) {
								new StuntScreen().replaceCurrent();
							}
						}
					}
					break;
				case 2:
					// 吸气

					if (BattleScreen.getInstance().p1().getGauge() < GameConstants.XIQI_GAUGE_COST) {
						new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
						new DummyStatus().pushToBattle();
						new PureViewScreen("你的怒气值不足！").replaceCurrent();
					} else {
						if (BattleScreen.getInstance().p1().skillsckd[3] < 10) {
							new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
							new PureViewScreen("你必须选择要用的内功心法").replaceCurrent();
						} else if(BattleScreen.getInstance().p1().getSp() >= BattleScreen.getInstance().p1().getHp()) {
							new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
							new PureViewScreen("你现在体力充沛。").replaceCurrent();
						} else if (BattleScreen.getInstance().p1().getFp() == 0) {
							new FreeStatus(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2()).pushToBattle();
							new PureViewScreen("你的内力不够。").replaceCurrent();
						} else {
							removeFromGame();
							BattleScreen.getInstance().xiqiprocess(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2());
						}
					}
					break;
				case 3:
					// 物品
					if (BattleScreen.getInstance().networkMode != NetworkMode.LOCAL) {
						new PureViewScreen("网络对战目前不能使用物品！").pushToGame();
						return;
					}
					new InventoryScreen(true, this).replaceCurrent();
					break;
				case 4:
					// 蓄力
					BattleScreen.getInstance().p1().addGauge(2);
					new ViewScreen(BattleScreen.bsp(BattleScreen.getInstance().p1(),
							BattleScreen.getInstance().p2(), "$N站立不动，暗中积蓄力量。")).replaceCurrent();
					break;
				case 5:
					// 逃跑

					if (BattleScreen.getInstance().networkMode != NetworkMode.LOCAL) {
						new GmudHttp().postBackground(GmudRestUrl.HostFinish, GmudHttp.initBattleParams(), null);
						getGame().popToRootScreen();
						return;
					}

					if (BattleScreen.getInstance().p2().haveTalent(132)) {
						new ViewScreen(BattleScreen.bsp(BattleScreen.getInstance().p1(),
								BattleScreen.getInstance().p2(), "$n一把拽住你，想跑，没门儿！")).replaceCurrent();
						return;
					}

					if (getGame().wulindahui != null) {
						// 认输（武林大会）
						BattleScreen.getInstance().eob = true;
						BattleOverScreen.tpflag = true;
						new ViewScreen("系统公告：" + BattleStart.name + "在与"
								+ BattleScreen.getInstance().p2().name + "的比赛中认输。").replaceCurrent();
						return;
					}

// if(Gmud.bs.p1().using(GmudGame.getInstance().xdata.get("SKILLID_凌波微步"))){
// Gmud.bs.eob = true;
// BattleOverScreen.tpflag = true;
// // GmudWorld.bs.p2().weak = -20;
// ViewScreen.setText("凌波微步毂纹生，你转眼间已经逃之夭夭。");
// game.setScreen(new ViewScreen(game));
// return;
// }

					if (BattleScreen.getInstance().p1().getDz() > 0) {
						BattleScreen.getInstance().p1().setDz(BattleScreen.getInstance().p1().getDz() - 1);
						new ViewScreen("你现在动弹不得！").replaceCurrent();
					} else if (BattleScreen.getInstance().p2().getDz() > 0) {
						getGame().popScreen();
						BattleScreen.getInstance().taopaoprocess(BattleScreen.getInstance().p2(),
								BattleScreen.bsp(BattleScreen.getInstance().p1(), BattleScreen.getInstance().p2(), "你趁$n呆若木鸡之时，脚底抹油，溜之大吉！"));
					} else {
						if (Gmud.rand.nextBoolean()) {
							new ViewScreen(BattleScreen.bsp(BattleScreen.getInstance().p1(),
									BattleScreen.getInstance().p2(), "$n一把拽住你，想跑，没门儿！")).replaceCurrent();
						} else {
							getGame().popScreen();
							BattleScreen.getInstance().taopaoprocess(BattleScreen.getInstance().p2(), "…………");
						}
					}
			}
			// BasicScreen.recheck();
		}
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.DialogScreen#onCancel()
	 */
	@Override
	public void onCancel() {

	}


	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#show()
	 */
	@Override
	protected void show() {
		BattleScreen.getInstance().present(0.01f);
		for (int i = 0; i < 6; i++) {
			buttons[i].draw();
		}
	}


	final NewButton cl[] = {NewButton.NB_UP, NewButton.NB_UP, NewButton.NB_DOWN,
			NewButton.NB_DOWN,
			NewButton.NB_LEFT, NewButton.NB_RIGHT, NewButton.NB_LEFT, NewButton.NB_RIGHT};

	final NewButton cl2[] = {NewButton.NB_UP, NewButton.NB_DOWN, NewButton.NB_UP,
			NewButton.NB_DOWN,
			NewButton.NB_RIGHT, NewButton.NB_LEFT, NewButton.NB_LEFT, NewButton.NB_RIGHT};

	int cc = 0;
	int cc2 = 0;

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.MenuScreen#onButtonClick(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	public void onButtonClick(NewButton b) {
		if (Gmud.DEBUG) {
			if (b == cl[cc]) {
				cc++;
				if (cc >= cl.length) {
					BattleScreen.getInstance().p2().dmg(10000, 0);
					cc = 0;
				}
			} else {
				cc = 0;
			}

			if (b == cl2[cc2]) {
				cc2++;
				if (cc2 >= cl2.length) {
					Npc npc = BattleScreen.getInstance().p2();

					for(int i = 0; i < npc.skills.length; i++){
						if(npc.skills[i] > 0){
							Logger.d("敌人技能%s为%d级", Gmud.skill[i].name, npc.skills[i]);
						}
					}

					String s = "";
					for(int i = 0; i < 5; i++){
						s += Gmud.skill[npc.skillsckd[i]].name + " ";
					}
					Logger.d("敌人装备武功为： %s", s);

					cc2 = 0;
				}
			} else {
				cc2 = 0;
			}
		}
		super.onButtonClick(b);
	}

}
